package com.zx.scp.controller;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zx.map.utils.SUConstants;
import com.zx.scp.service.IncomHandleService;
import com.zx.scp.service.model.OrderPayCallBackModel;

@Controller
@RequestMapping("/income/alipay")
public class AlipayIncomeController {
	@Autowired
	private IncomHandleService incomHandleService;
	  private static Logger log = Logger.getLogger(AlipayIncomeController.class);
	/**
	 * 通知支付
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/nofityPay")
	public void nofityPay(HttpServletRequest request, HttpServletResponse response) {
		try {
			if (request.getParameter("trade_status") != null
					&& "TRADE_SUCCESS".equals(request.getParameter("trade_status"))) {
				log.info(request.getQueryString());
				OrderPayCallBackModel omodel = new OrderPayCallBackModel();
				omodel.setOrdId(request.getParameter("out_trade_no"));
				omodel.setPaymentAmt(Double.parseDouble(request.getParameter("buyer_pay_amount")));
				omodel.setPaymentTime(request.getParameter("gmt_payment").replaceAll("-", "").replaceAll(" ", "")
						.replaceAll(":", ""));
				omodel.setPaymentType(SUConstants.paymentTypeAlipay);
				omodel.setTransStatus("S");
				omodel.setUserId(request.getParameter("buyer_id"));
				omodel.setUserName(request.getParameter("buyer_logon_id"));
				omodel.setPaymentId(request.getParameter("trade_no"));
				incomHandleService.handleOrderCallBack(omodel);
			
			}
			response.setCharacterEncoding("utf-8");  
			response.setHeader("content-type", "text/html;charset=utf-8");  
			OutputStream out = response.getOutputStream();
			out.write("success".getBytes("utf-8"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
